Res_Plist   = 0x01
Res_Png = 0x02
Res_Bgm = 0x03
Res_Effs    = 0x04

-----------SpriteNode

-------UI
s_MainUI2 = GetPathRes("mainUI2.png")

-- 游戏界面
s_MainBG = GetPathRes("mainBG.png")

s_ShoujiBg  = "bg2_sj.png"
s_ShoujiBg2 = "bg2_sj2.png"
s_infoBarR = "UIdaoju024.png"
s_Achive_bg2    = "UIjiemianchengjiu_001.png"
s_Achive_bg3    = "UIjiemianchengjiu_004.png"
s_Achive_title  = "UIjiemianchengjiu_003.png"

s_Space_bg  = "UIjiemianditu_001.png"
s_Space_bgHead = "UIjiemianditu_002.png"
s_Space_sbg = "UIzhujiemian_001.png"
s_Space_Setingbg= "UIzhujiemian_006.png"
s_Space_ShopZCbg= "UIzhujiemian_003.png"

s_Game_WinImg   = "UIyouxizhong_004.png"
s_Game_LostImg  = "UIyouxizhong_001.png"
s_Game_WinImgBG = "UIyouxizhong_005.png"
s_Game_LostImgBG= "UIyouxizhong_006.png"

s_Profess_LvbgSk = "UIjiemianlever_003.png"
s_Profess_lvbgPy = "UIjiemianlever_004.png"
s_ShopBg    = "UIjiemianshop_001.png"


local layer = nil
-- create menu
local function initWithLayer()
    -- 加载资源
    local _lb_loading = CCLabelTTF:create(table.concat({"游戏正在加载中……"}), "Arial", FontSize)
    _lb_loading:setPosition(gdccp(DWinSizeWidth/2, 250))
    layer:addChild(_lb_loading, LayerIdx_Gui)
    
    
    ---------------------------
    local _loadingEnd = false
    local _loadingSums = 0 --加载总数
    local _loadingFine = 0 --完成数量
    local _joinGame = false
    local _baseLayer_entry = nil
    
    
    local _resTable = {}
    
    local function addload(rtype, arg1, arg2)
        local res = {
            rtype=rtype,
            arg1=arg1,
            arg2=arg2
        }
        table.insert(_resTable, res)
        _loadingSums = _loadingSums + 1
    end
    local function loading_next()
        local _nowload = nil
        for k, res in pairs(_resTable) do
            _nowload = res
            _resTable[k] = nil
            break
        end
        
        if _nowload == nil then
            ----------------加载完成
            _lb_loading:setString("")
            
            
            Play_BackgroundMusic(sm_Menu, true, true)
        
            _loadingEnd = true
            
            
            --进入游戏
            local sp_bg = CCSprite:createWithSpriteFrameName("Txtenter.png")
            sp_bg:setPosition(gdccp(DWinSizeWidth/2, 250))
            layer:addChild(sp_bg,2)
            
            local arr1 = CCArray:create()
            arr1:addObject(CCFadeTo:create(1.0, 50))
            arr1:addObject(CCFadeTo:create(1.0, 255))
            sp_bg:runAction(CCRepeatForever:create(CCSequence:create(arr1)))
            
            --显示总使用内存
            print(CCTextureCache:sharedTextureCache():dumpCachedTextureInfo())
            
            
    
            -- 初始化配置文件
            if CCUserDefault:sharedUserDefault():getIntegerForKey("first") == 0 then
                CCUserDefault:sharedUserDefault():setIntegerForKey("first", 1)
                CCUserDefault:sharedUserDefault():flush()
                
                AddTili(Tili_Max)
                --- 游戏补单
                --toJaveInitBuys()
            end
            
    
            --游戏初始化了
            toJaveInitGames()
            return
        end
        
        local rtype = _nowload.rtype
        local arg1 = _nowload.arg1
        local arg2 = _nowload.arg2
        if rtype == Res_Plist then
            if arg1 ~= nil and arg2 ~= nil then
                CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile(arg1, arg2)
            else
                print("error:load res is nil",rtype, arg1, arg2)
            end
        elseif rtype == Res_Png then
            if arg1 ~= nil then
                CCTextureCache:sharedTextureCache():addImage(arg1)
            else
                print("error:load res is nil",rtype, arg1, arg2)
            end
        elseif rtype == Res_Bgm then
            SimpleAudioEngine:sharedEngine():preloadBackgroundMusic(GetFiles(arg1))
        elseif rtype == Res_Effs then
            SimpleAudioEngine:sharedEngine():preloadEffect(GetFiles(arg1))
        end
        
        print("load..",arg1)
        
        _loadingFine = _loadingFine + 1
        _lb_loading:setString(table.concat({"游戏正在加载中…… ",string.format("%.0f", (_loadingFine/_loadingSums)*100),"%"}))
    end
    
    --- 音乐音效
    for file, v in pairs(SoundTypeTabls) do
        if v == 1 then
            addload(Res_Bgm, file)
        elseif v == 2 then
            addload(Res_Effs, file)
        end
        --print(file,v)
    end
    --print("SoundTypeTabls",SoundTypeTabls)
    
    
    CCConnectionData:sharedConnectionData():addData(GetPathRes("fmodel/role.xml"), GetPathRes("fmodel/rolet.xml"))
    -- 需要预加载的资源
    addload(Res_Plist, GetPathRes("mainUI.plist"), GetPathRes("mainUI.png"))
    addload(Res_Plist, GetPathRes("mainUI2.plist"), s_MainUI2)
    -- 游戏中资源
    addload(Res_Plist, GetPathRes("fmodel/role.plist"), GetPathRes("fmodel/role.png"))
    addload(Res_Plist, GetPathRes("gameUI.plist"), GetPathRes("gameUI.png"))
    
    addload(Res_Png, GetNumberFile(vNumberTime))
    addload(Res_Png, GetNumberFile(vNumberStage))
    addload(Res_Png, GetNumberFile(vNumberScore))
    addload(Res_Png, GetNumberFile(vNumberHurt))
    addload(Res_Png, GetNumberFile(vNumberCrytal))
    addload(Res_Png, GetNumberFile(vNumberCombo))
    
    
    SimpleAudioEngine:sharedEngine():preloadBackgroundMusic(GetFiles(sm_Menu))
    
    ------------ 火花特效
    local _lasthuoht = os.clock()
    local _lastshow = 0
    local _huohtable = {}
    local huotable = {"effhuohua001.png","effhuohua002.png","effhuohua001.png","effhuohua002.png","effhuohua001.png","effhuohua002.png","effhuohua003.png"}
    local function delhuoh()
    end
    
    local function updateHuoh(dt)
        if _lasthuoht + _lastshow <= os.clock() then
            --
            local x = 0
            local y = 0
            --if math.random(0,1) == 1 then
            --    x = math.random(25, DWinSizeWidth)
            --    y = DWinSizeHeight
            --else
            --    x = DWinSizeWidth
            --    y = math.random(25, DWinSizeHeight)
            --end
            if math.random(0,1) == 1 then
                x = math.random(0, gdv(DWinSizeWidth-25))
                y = 0
            else
                x = 0
                y = math.random(0, gdv(DWinSizeHeight-25))
            end
            local sfile = huotable[math.random(1, #huotable)]
            local sprite = CCSprite:createWithSpriteFrameName(sfile)
            --sprite:setAnchorPoint(ccp(0,0))
            sprite:setAnchorPoint(ccp(1,1))
            sprite:setPosition(ccp(x,y))
            layer:addChild(sprite,1)
            table.insert(_huohtable, sprite)
            
            local arr1 = CCArray:create()
            arr1:addObject(CCFadeTo:create(0.3, 200))
            arr1:addObject(CCFadeTo:create(1.5, 255))
            sprite:runAction(CCRepeatForever:create(CCSequence:create(arr1)))
            --Tag为速度
            sprite:setTag(math.random(gdv(50),gdv(150)))
            
            _lastshow = math.random(0.3,1.0)
            _lasthuoht = os.clock()
        end
        
        table.foreach(_huohtable, function(k, sprite)
            local x,y = sprite:getPosition()
            local speed =  sprite:getTag() * dt
            x = x + 0.9 * speed
            y = y + 0.6 * speed
            sprite:setPosition(ccp(x, y))
            if x > gdv(DWinSizeWidth+100) or y > gdv(DWinSizeHeight+100) then --x < -100 or y < -100 then
                layer:removeChild(sprite, true)
                _huohtable[k] = nil
            end
        end)
    end
    
    
    
    local _frame = 0
    local function update(dt)
        --print(_frame)
        -- 每次加载一个资源
        if _loadingEnd then
            
            updateHuoh(dt)
            
            return
        end
        
        if _frame == 0 then
            -- 加载音效音乐
            Init_GameSound()
            
            
        end
        
        loading_next()
        _frame = _frame + 1
    end
    
    ---暂停游戏
    
    local function SpriteEase_onEnterOrExit(tag,tag2)
        if not _joinGame then
            _joinGame = true
            
            _baseLayer_entry = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(update,1.0/60.0,false)
        else
            _joinGame = false
            
            if _baseLayer_entry ~= nil then
                CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(_baseLayer_entry)
            end
            _baseLayer_entry = nil
        end
    end
    
    
    -- 触摸回调响应
    local function onTouch(eventType, x, y)
        if _loadingEnd then
            GameReplaceScne(createLayerMenu())
        end
    end
    
    -- 接受触摸
    layer:setTouchEnabled(true)
    -- 触摸回调
    layer:registerScriptTouchHandler(onTouch)
    layer:registerScriptHandler(SpriteEase_onEnterOrExit)
    
    
    
    
    
    CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile(GetPathRes("Loadui.plist"), GetPathRes("Loadui.png"))
    
    --背景
    local sp_bg = nil
    sp_bg = CCSprite:createWithSpriteFrameName("mainBG.png")
    sp_bg:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2))
    layer:addChild(sp_bg,1)
    
    --print("___",DWinSize.width, DWinSize.height)
    
    ---加载成就状态
    Load_AllAchieves()
end

function createLoading()
    local scene = CCScene:create()
    layer = CCLayer:create()

    initWithLayer()
    
    scene:addChild(layer)

    return scene, UI_Load
end














